package com.lianzhihui.romeex.ui.wallet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.inter.SelectListener;
import com.lianzhihui.romeex.inter.SetListener;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.CoinUtils;
import com.lianzhihui.romeex.utils.ImagesUtil;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.QrUtils.QRUtils;
import com.lianzhihui.romeex.utils.UserUtils;
import com.lianzhihui.romeex.widget.dialog.CoinSelDialog;
import com.lianzhihui.romeex.widget.dialog.SetEtDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 收款界面
 * Author: RAMON
 * CreateDate: 2020/11/6 11:37 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ShouqianActivity extends BaseActivity {

    @BindView(R.id.ll_select)
    LinearLayout ll_select;
    @BindView(R.id.ll_qr_range)
    LinearLayout ll_qr_range;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_sel)
    TextView tv_sel;
    @BindView(R.id.tv_sum)
    TextView tv_sum;
    @BindView(R.id.tv_type2)
    TextView tv_type2;
    @BindView(R.id.tv_warning)
    TextView tv_warning;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;

    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.iv_copy)
    ImageView iv_copy;
    @BindView(R.id.bt_set)
    Button bt_set;
    @BindView(R.id.bt_save)
    Button bt_save;

    WalletInfo walletInfo;
    double sum = 0D;
    WalletMainBean.CoinBean coin = new WalletMainBean.CoinBean();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shouqian_layout;
    }

    @Override
    protected void initView() {
        setTitle("收款");
        Intent intent = getIntent();
        coin = (WalletMainBean.CoinBean) intent.getSerializableExtra("coin");
        if (coin == null) {
            coin = CoinUtils.getDefCoin();
        }
        setOption("收款记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, TradeRecordActivity.class);
                intent.putExtra("coinName", coin.getCoinName());
                intent.putExtra("coinNameCobo", coin.getCoinNameCobo());
                intent.putExtra("coinNameAlias", coin.getCoinNameAlias());
                intent.putExtra("tradeType", "2");
                startActivity(intent);
            }
        });
        setType();
        walletInfo = UserUtils.getCurrentUser();
        tv_address.setText(walletInfo.getRoAddress());
        madeQR();
        setWarning();
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.ll_select, R.id.iv_copy, R.id.bt_set, R.id.bt_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_select:

                //选择币种
                CoinSelDialog dialog = new CoinSelDialog(this);
                dialog.setListener(new SelectListener() {
                    @Override
                    public void onSelect(int position) {
                        coin = dialog.adapter.getItem(position);
                        setType();
                        madeQR();
                        setWarning();
                        dialog.dismiss();

                    }
                });
                dialog.show();

                break;
            case R.id.iv_copy:
                copyString(tv_address.getText().toString());
                break;
            case R.id.bt_set:
                if (sum != 0) {
                    sum = 0D;
                    tv_sum.setText("");
                    madeQR();
                    bt_set.setText("设置数量");
                    return;
                }
                //设置数量
                SetEtDialog seDialog = new SetEtDialog(this);
                seDialog.setListener(new SetListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void getInput(double content) {
                        sum = content;
                        if (sum <= 0) {
                            sum = 0;
                            tv_sum.setText("");
                        } else {
                            tv_sum.setText(content + "");
                        }
                        madeQR();
                        seDialog.dismiss();
                    }
                });
                seDialog.show();
                break;
            case R.id.bt_save:
                save();
                break;
        }

    }

    private void setWarning() {
        String s = null != coin ? coin.getCoinNameAlias() : "";
        String string = getString(R.string.shouqian_warning_p1) + s +
                getString(R.string.shouqian_warning_p2);
        tv_warning.setText(string);
    }

    /**
     * 生成二维码
     */
    private void madeQR() {
        if (coin != null && !TextUtils.isEmpty(coin.getCoinNameAlias())) {
            String s1 = walletInfo == null ? "" : (TextUtils.isEmpty(walletInfo.getAddress()) ? "" : walletInfo.getAddress());
            String s2 = coin == null ? "" : (TextUtils.isEmpty(coin.getCoinNameAlias()) ? "" : coin.getCoinNameAlias());
            String addrWithValue = QRUtils.QR_CODE_WALLET_RO_FLAG + QRUtils.QR_CODE_VALUE_FLAG + sum
                    + "_" + s1 + "_" + s2;
            LogUtil.error("ramon", "addrWithValue: " + addrWithValue);
            ImagesUtil.createQrCodeImg(addrWithValue, iv_qrcode, 220);
            if (sum != 0) {
                bt_set.setText("清除设置");
            }
        }


    }

    public void setType() {
        if (null != coin) {
            tv_type.setText(coin.getCoinNameAlias());
            tv_type2.setText(coin.getCoinNameAlias());
        }
    }

    private void save() {
        if (NoDoubleClickUtils.isDoubleClick()) {
            String[] PERMISSIONS = {
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.WRITE_EXTERNAL_STORAGE"};
            //检测是否有写的权限
            int permission = ContextCompat.checkSelfPermission(this,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
            } else {
                ImagesUtil.saveQrCode(this, ll_qr_range);
            }
        }
    }

}
